/**
 * Created by TomSean on 2018/1/10.
 */
var ParkOrgGroupManager = function ($parent) {
    this.sysUtils = new kendoUtils();
    this.$parent = $parent;
    this.init();
    return this;
}
ParkOrgGroupManager.prototype = {
    init : function () {
        var $this = this;
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent);
        var functionArray = [
        ];
        this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
        this.findData();
    },
    findData : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        $this.deployPermission();
        $this.loadWorkStation();
        $this.deployGroup();
    },
    loadWorkStation : function () {
        var $this = this;
        JsonpAjax(baseApiPath+"park_org_group/work_station", '', 'json', function(res) {
            var htmlStr = '';
            console.log(res);
            if (isEmpty(res)) {
                htmlStr = '暂无数据';
                notification({'title':'提示', 'message':'没有通道数据，请先在车场组织里配置通道'}, 'info');
            } else {
                var list = res;
                htmlStr = '<ul>';
                for (var i = 0; i < list.length; i++) {
                    if (list[i].orgLevel==0) {
                        htmlStr += '<li>';
                        htmlStr += '    <label><input type="checkbox" name="chn-code" data-type="'+list[i].organizationTypeCode+'" value="'+list[i].organizationCode+'" /> '+list[i].organizationName+'</label>';
                        htmlStr = $this.iterationWorkStation(htmlStr, list, list[i].organizationCode);
                        htmlStr += '</li>';
                    }
                }
                $this.iterationWorkStation();
                htmlStr += '</ul>';
            }
            $this.$parent.find('.slide-open-item.template .bind-chn').append(htmlStr);
        }, false);
    },
    iterationWorkStation : function(str, list, parentCode) {
        if (isEmpty(list)) {
            return str;
        }
        str += '<ul>';
        var $this = this;
        for (var i = 0; i < list.length; i++) {
            if (list[i].parentCode==parentCode) {
                str += '<li>';
                str += '<label><input type="checkbox" name="chn-code" data-type="'+list[i].organizationTypeCode+'" value="'+list[i].organizationCode+'" /> '+list[i].organizationName+'</label>';
                str = $this.iterationWorkStation(str, list, list[i].organizationCode);
                str += '</li>';
            }
        }
        str += '</ul>';
        return str;
    },
    deployPermission : function () {
        var permissionArray = Utils.getModelArrayByParentModelKey(BASIS.TRAFFIC_TYPE_CLASS.ROOT);
        var liStr = '<li>';
        for (var i = 0; i < permissionArray.length; i++) {
            liStr += '<label><input type="checkbox" value="'+permissionArray[i].modelValue+'" />'+permissionArray[i].modelName+'</label>';
        }
        liStr += '</li>';
        var template = this.$parent.find('.slide-open-item.template');
        template.find('.ckIE').html(liStr);
        template.find('.ckRepeatIE').html(liStr);
        template.find('.fullStopIn').html(liStr);
    },
    deployGroup : function () {
        var $this = this;
        JsonpAjax(baseApiPath + 'park_org_group/find_group', '', 'json', function (res) {
            if (isEmpty(res)) {
                notification({'title':'警告', 'message':'当前暂无权限分组'}, 'info');
                return;
            }
            var list = res;
            for (var i = 0; i < list.length; i++) {
                var item = $this.$parent.find('.slide-open-item.template').clone(true);
                item.removeClass('template');
                item.find('input[name="accpet"]').each(function (inx, itm) {
                    $(itm).attr("name", "accpet-"+list[i].ID);
                })
                item.find('.slide-open-menu.panel').attr('data-id', list[i].ID);
                item.find('.group-title').html(list[i].GROUP_NAME);
                item.find('.group-title-input').val(list[i].GROUP_NAME);
                var ckIE = list[i].CK_IE.split(',');
                var ckRepeatIE = list[i].CK_REPEAT_IE.split(',');
                var fullStopIn = list[i].FULL_STOP_IN.split(',');
                for (var j = 0; j < ckIE.length; j++) {
                    item.find('.ckIE input[type="checkbox"][value="'+ckIE[j]+'"]').prop('checked', 'checked').trigger('change');
                    item.find('.ckRepeatIE input[type="checkbox"][value="'+ckRepeatIE[j]+'"]').prop('checked', 'checked').trigger('change');
                    item.find('.fullStopIn input[type="checkbox"][value="'+fullStopIn[j]+'"]').prop('checked', 'checked').trigger('change');
                }
                var chnArray = list[i].CHN_ID.split(',');
                for (var j = 0; j < chnArray.length; j++) {
                    item.find('.bind-chn input[type="checkbox"][value="'+chnArray[j]+'"]').prop('checked', 'checked').trigger('change');
                }
                item.show();
                $this.$parent.find('.slide-open-item.create').before(item);
            }
        });
    },
    createGroup : function (t, e) {
        var $this = this;
        var item = $this.$parent.find('.slide-open-item.template').clone(true);
        item.removeClass('template');
        item.find('input[name="accpet"]').each(function (inx, itm) {
            $(itm).attr("name", "accpet-"+new Date().getMilliseconds());
        })
        item.show();
        $(t).before(item);
    },
    showAdvance : function (t) {
        if ($(t).attr('data-value') == 0) {
            $(t).parents('li:eq(0)').find('.slide-open-content').show();
            $(t).html('隐藏');
            $(t).attr('data-value', 1);
        } else {
            $(t).parents('li:eq(0)').find('.slide-open-content').hide();
            $(t).html('显示高级');
            $(t).attr('data-value', 0);
        }
    },
    changeGroupTitleEvent : function (t, e) {
        if (isEmpty($(t).val())) {
            $(t).parents('.slide-open-menu:eq(0)').siblings('.group-title').html('新建权限分组');
        } else {
            $(t).parents('.slide-open-menu:eq(0)').siblings('.group-title').html($(t).val());
        }
    },
    saveGroupSetting : function (t, e) {
        var $this = this;
        var panel = $(t).parents('.slide-open-menu.panel');
        var groupId = panel.attr('data-id');
        var groupName = panel.find('.group-title-input').val();
        var ckIE = '';
        panel.find('.ckIE input[type="checkbox"]:checked').each(function (inx, itm) {
            ckIE += $(itm).val() + ',';
        });
        if (!isEmpty(ckIE)) ckIE = ckIE.substring(0, ckIE.length-1);

        var ckRepeatIE = '';
        panel.find('.ckRepeatIE input[type="checkbox"]:checked').each(function (inx, itm) {
            ckRepeatIE += $(itm).val() + ',';
        });
        if (!isEmpty(ckRepeatIE)) ckRepeatIE = ckRepeatIE.substring(0, ckRepeatIE.length-1);

        var fullStopIn = '';
        panel.find('.fullStopIn input[type="checkbox"]:checked').each(function (inx, itm) {
            fullStopIn += $(itm).val() + ',';
        });
        if (!isEmpty(fullStopIn)) fullStopIn = fullStopIn.substring(0, fullStopIn.length-1);

        var chnArray = '';
        panel.find('.bind-chn input[type="checkbox"]:checked').each(function (inx, itm) {
            if ($(itm).attr('data-type').indexOf('chn') != -1) {
                chnArray += $(itm).val() + ',';
            }
        });
        if (!isEmpty(chnArray)) chnArray = chnArray.substring(0, chnArray.length-1);
        var dataT = {'groupId':groupId, 'groupName':groupName, 'ckIE':ckIE, 'ckRepeatIE':ckRepeatIE, 'fullStopIn':fullStopIn, 'chnArray':chnArray};
        JsonpAjax(baseApiPath + 'park_org_group/save_group', JSON.stringify(dataT), 'json', function (res) {
            panel.attr('data-id', res.groupId);
        }, false);
    },
    deleteGroup : function (t, e) {
        var $this = this;
        var id = $(t).parents('.slide-open-menu.panel').attr('data-id');
        if (isEmpty(id)) {
            $(t).parents('.slide-open-menu.panel').parents('.slide-open-item:eq(0)').remove();
            return;
        }
        notification({title:"确认信息", message:"确认删除该分组？通行权限保持不变"}, 'confirm', {
            callback : function(){
                JsonpAjax(baseApiPath + 'park_org_group/delete_group/' + id, '', 'json', function (res) {
                    $(t).parents('.slide-open-menu.panel').parents('.slide-open-item:eq(0)').remove();
                    $this.$parent.find('.slide-open-menu.top .slide-open-item').not('.slide-open-item.create').not('.slide-open-item.template').show();
                }, false);
            }
        });

    }
}